package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.bean.CategoriesBean;
import com.apps.ftapp.model.SubjectModel;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by lyli on 2017/10/3.
 */

public class IndexGridAdapter extends BaseAdapter {

    private Context mContext;
    private List<CategoriesBean> dataList;

    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;

    public IndexGridAdapter(Context mContext, List<CategoriesBean> dataList) {
        super();
        this.mContext = mContext;
        this.dataList = dataList;

        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));

    }

    @Override
    public int getCount() {
        if (null == dataList) {
            return 0;
        } else {
            if (this.dataList.size() > 8) {
                return 8;
            }
            return this.dataList.size();
        }
    }

    @Override
    public Object getItem(int i) {
        if (dataList == null) {
            return null;
        } else {
            return this.dataList.size();
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        CategoriesBean bean = dataList.get(i);

        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(this.mContext).inflate(R.layout.fragment_index_gridview_item, null, false);
            holder.imageView = view.findViewById(R.id.imageView);
            holder.textView = view.findViewById(R.id.textView);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        //holder.imageView.setImageResource(imgs[i]);
        imageLoader.displayImage(bean.getIcon(), holder.imageView, options);
        holder.textView.setText(bean.getName());

        return view;
    }


    private class ViewHolder {
        ImageView imageView;
        TextView textView;
    }


}
